package com.activeMQ.property;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.jms.Message;

import net.sf.json.JSONObject;

import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.activeMq.receiver.ITopicMsgHandler;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.cerHouseholder.model.CerHouseholderDto;

/***
 * 接收添加业主的信息
 * 
 * @author shendiao
 * 
 */
public class Owner implements ITopicMsgHandler {

	private static Logger logger = Logger.getLogger(Owner.class);

	/***
	 * 监听的业主变动信息
	 */
	@Override
	public void onMsgRecv(Message message) throws Exception {

		try {

			/***
			 * 获取spring 的上下文
			 */
			WebApplicationContext wac = ContextLoader

			.getCurrentWebApplicationContext();

			if (wac != null) {

				/***
				 * 初始化业主实例
				 */

				ICerHouseholderService cerHouseholderService = (ICerHouseholderService) wac
						.getBean("cerHouseholderService");
				/***
				 * 获取接收的消息
				 */
				ActiveMQMapMessage activeMQMapMessage = (ActiveMQMapMessage) message;

				/***
				 * 获取jms类型
				 */
				String messageType = activeMQMapMessage.getJMSType();

				/***
				 * 获取接收者类型
				 */
				Map<String, Object> receiver = activeMQMapMessage
						.getProperties();

				/***
				 * 获取发送的内容
				 */
				Map<String, Object> paMap = activeMQMapMessage.getContentMap();

				if (receiver.containsKey("receiver")
						&& !StringUtils.isBlank(messageType)) {
					if (paMap != null && paMap.size() > 0) {

						/***
						 * 新增业主
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_ADD_OWNER")
								&& messageType.equals("RECEIVER_ADD_OWNER")) {
							if (paMap.containsKey("ownerInfo") == true) {
								JSONObject jsonObject = JSONObject
										.fromObject(paMap.get("ownerInfo")
												.toString());

								/****
								 * 添加业主
								 */
								CerHouseholderDto cerHouseholderDto = new CerHouseholderDto();
								cerHouseholderDto.setApartmentID(jsonObject
										.getString("apartmentID"));
								cerHouseholderDto
										.setLinkAptHouseholderID(jsonObject
												.getString("linkAptHouseholderID"));
								cerHouseholderDto.setName(jsonObject
										.getString("name"));
								cerHouseholderDto.setPhone(jsonObject
										.getString("phone"));
								cerHouseholderDto.setType(jsonObject
										.getString("type"));

								List<CerHouseholderDto> cerHouseholderDtos = cerHouseholderService
										.findCerHouseholderByRelevantID(
												null,
												cerHouseholderDto
														.getApartmentID(),
												cerHouseholderDto.getType(),
												cerHouseholderDto
														.getLinkAptHouseholderID(),
												null, null, null);
								if (cerHouseholderDtos != null
										&& cerHouseholderDtos.size() > 0) {
									logger.error("该业主信息已经存在(消息体 "
											+ JSONObject.fromObject(paMap)
													.toString() + ")");
									throw new AppBizException(2, "该业主信息已经存在");
								}
								cerHouseholderService
										.addCerHouseholder(cerHouseholderDto);

								System.out.println(JSONObject.fromObject(paMap)
										.toString());
								logger.info("在"
										+ new Date()
										+ "添加业主信息(消息体 "
										+ JSONObject.fromObject(
												paMap.get("ownerInfo"))
												.toString() + ")");

							}
						}

						/***
						 * 修改业主
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_EDIT_OWNER")
								&& messageType.equals("RECEIVER_EDIT_OWNER")) {

							if (paMap.containsKey("ownerInfo") == true) {
								JSONObject jsonObject = JSONObject
										.fromObject(paMap.get("ownerInfo"));

								/****
								 * 修改业主
								 */
								CerHouseholderDto cerHouseholderDto = new CerHouseholderDto();
								cerHouseholderDto.setApartmentID(jsonObject
										.getString("apartmentID"));
								cerHouseholderDto
										.setLinkAptHouseholderID(jsonObject
												.getString("linkAptHouseholderID"));
								cerHouseholderDto.setName(jsonObject
										.getString("name"));
								cerHouseholderDto.setPhone(jsonObject
										.getString("phone"));
								cerHouseholderDto.setType(jsonObject
										.getString("type"));

								List<CerHouseholderDto> cerHouseholderDtos = cerHouseholderService
										.findCerHouseholderByRelevantID(
												null,
												cerHouseholderDto
														.getApartmentID(),
												cerHouseholderDto.getType(),
												cerHouseholderDto
														.getLinkAptHouseholderID(),
												null, null, null);
								if (cerHouseholderDtos != null
										&& cerHouseholderDtos.size() > 1) {
									logger.error("该业主信息已经存在(消息体 "
											+ JSONObject.fromObject(paMap)
													.toString() + ")");
									throw new AppBizException(2, "该业主信息已经存在");
								}

								if (cerHouseholderDtos != null
										&& cerHouseholderDtos.size() > 0) {
									cerHouseholderDto
											.setHouseholderID(cerHouseholderDtos
													.get(0).getHouseholderID());
									cerHouseholderService
											.editCerHouseholder(cerHouseholderDto);
								}
								logger.info("在"
										+ new Date()
										+ "修改业主信息(消息体 "
										+ JSONObject.fromObject(
												paMap.get("ownerInfo"))
												.toString() + ")");

							}

						}

						/***
						 * 删除业主
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_DELETE_OWNER")
								&& messageType.equals("RECEIVER_DELETE_OWNER")) {
							if (paMap.containsKey("linkAptHouseholderID") == true) {
								// linkAptHouseholderID
								cerHouseholderService
										.deleteCerHouseholderBylinkID(paMap
												.get("linkAptHouseholderID")
												.toString());
								logger.info("在"
										+ new Date()
										+ "删除授权ID为"
										+ paMap.get("linkAptHouseholderID")
												.toString()
										+ "的授权信息(消息体 "
										+ JSONObject.fromObject(paMap)
												.toString() + ")");

							}
						}
					}
				}
			}

		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}
}
